from selenium.webdriver import Chrome
from selenium.webdriver.common import by
from selenium.webdriver.common import keys
import time
import requests
import os


def test_connect_req(url):  # 测试网络连接
    try:
        requests.get(url, timeout=5)
        return True
    except Exception as e:
        return False


def test_connection_byPing():  # ping测试网络连接
    res = os.system("ping www.baidu.com -n 3")
    if res == 0:
        return True
    else:
        return False


def logout():  # 下线已登录的账号
    pass


if __name__ == '__main__':
    # url = "http://10.0.11.5"
    url = "http://10.0.11.5/eportal/index.jsp?wlanuserip=884ee7e8600173e035b5af8f032b1e6c&wlanacname=6ed21d06b9e4d9c468a3370f1adc5501&ssid=&nasip=1ae062d3cecb874cf5e3b9ec93a46392&snmpagentip=&mac=3de86dad0e92734574e2489e6499e3d6&t=wireless-v2&url=709db9dc9ce334aa02a9e1ee58ba6fcf3bc3349e947ead368bdd021b808fdbac30c65edaa96b0727&apmac=&nasid=6ed21d06b9e4d9c468a3370f1adc5501&vid=ab1b892ceaadef6c&port=182b3259bf8f9b96&nasportid=5b9da5b08a53a54049a4dd4214efc555fd611d3643ecb79fe66cc1c78ddd9671"
    ip = "10.0.11.5"
    username = "21050539333"
    password = "h123456"

    web = Chrome()
    # 测试校园网连接

    # res = test_connect_req(url)
    # print(f"校园网连接：{res}")
    web.maximize_window()
    web.get(url)
    time.sleep(1)  # 加载页面

    # 输入用户名、密码
    user_el = web.find_element(by.By.XPATH, '//*[@id="username"]')
    user_el.send_keys(username)
    user_el.send_keys(keys.Keys.TAB)

    pwd_el = web.find_element(by.By.XPATH, '//*[@id="pwd"]')
    pwd_el.send_keys(password, keys.Keys.ENTER)
    time.sleep(1)  # 加载页面

    # 设置睡眠时间
    time.sleep(10000)
